package com.it.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Component("myAspect")
@Aspect
public class MyAspect {
    //切入点表达式的public修饰符可以不写
    @Pointcut("execution(public * com.it.service.impl.*.*(..))")
    public void pointcut(){};

    @Before("pointcut()")
    public void before() {
        System.out.println("before..");
    }

    @AfterReturning("pointcut()")
    public void afterReturning() {
        System.out.println("afterReturning...");
    }

    @AfterThrowing("pointcut()")
    public void afterThrowing() {
        System.out.println("afterThrowing...");
    }

    @After("pointcut()")
    public void after() {
        System.out.println("after...");
    }

    @Around("pointcut()")
    public Object around(ProceedingJoinPoint pjp) {

        Object[] args = pjp.getArgs();
        System.out.println(Arrays.toString(args));

        Object proceed=null;
        try {
            System.out.println("前置。。");

            proceed = pjp.proceed();
            System.out.println("后置..");
        } catch (Throwable throwable) {
            System.out.println("异常。。");
        } finally {
            System.out.println("最终..");
        }
        return proceed;
    }
}
